package org.raymond.iworks.study.spring.service;

import lombok.extern.slf4j.Slf4j;
import org.raymond.iworks.commons.utils.RandomValueUtil;
import org.raymond.iworks.commons.utils.SnowFlake;
import org.raymond.iworks.repository.entity.TSysLog;
import org.raymond.iworks.repository.entity.TSysUser;
import org.raymond.iworks.repository.repository.TSysLogRepository;
import org.raymond.iworks.repository.repository.TSysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
@Service
public class BaseTestService {
    @Autowired
    private TSysLogRepository tSysLogRepository;
    @Autowired
    private TSysUserRepository tSysUserRepository;

    @Transactional(propagation= Propagation.MANDATORY, rollbackFor = Exception.class)
    public void mandatoryTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.mandatory");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }
    @Transactional(propagation= Propagation.NEVER, rollbackFor = Exception.class)
    public void neverTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.never");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }
    @Transactional(propagation= Propagation.SUPPORTS, rollbackFor = Exception.class)
    public void supportsTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.supports");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }
    @Transactional(propagation= Propagation.NOT_SUPPORTED, rollbackFor = Exception.class)
    public void notSupportedTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.not_supported");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }
    @Transactional(propagation= Propagation.NESTED, rollbackFor = Exception.class)
    public void nestedTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.nested");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }

    @Transactional(propagation= Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void requiredNewTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.required_new");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }

    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public void requiredTest(boolean ex) throws Exception {
        TSysLog sysLog = newLog("propagation.required");
        tSysLogRepository.save(sysLog);
        if(ex) {
            throw new Exception("throws ex");
        }
    }

    public TSysLog newLog(String request){
        Date current = new Date();
        TSysLog log = TSysLog.builder()
                .id(SnowFlake.generateId())
                .method("POST")
                .clientTime(current)
                .startTime(current)
                .endTime(current)

                .request(request)
                .build();
        return log;
    }

    private TSysUser newUser(){
        TSysUser user = buildRandomUser();
        return user;
    }

    private String[] sexs = {"secret", "male", "female"};

    private TSysUser buildRandomUser(){
        TSysUser tSysUser = TSysUser.builder()
                .username(randomUsername())
                .password("123456")
                .mobile(randomMobile())
                .countryCode("+86")
                .sex(randomSex())
                .email(randomEmail())
                .birthday(randomBirthday())
                .source("system")
                .build();
        return tSysUser;
    }

    // 8~20位,大小写字母,数字,特殊字符(~!@#$%^&-), 非数字开头
    private String randomUsername(){
        return "匿名用户-"+ RandomValueUtil.getStr(10, 11);
    }

    private String randomEmail(){
        return RandomValueUtil.getEmail(5, 12);
    }

    private String randomMobile(){
        return RandomValueUtil.getTel();
    }

    private String randomSex(){
        return sexs[RandomValueUtil.getInt(0,3)];
    }

    private Date randomBirthday(){
        return RandomValueUtil.getDate("1970-01-01 00:00:00", "2004-01-01 00:00:00");
    }
}
